package com.example.test.comutil;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.text.Selection;
import android.text.Spannable;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.annotation.RequiresApi;

/**
 * 关闭和打开软键盘
 * Created by Administrator on 2016/8/3.
 */
public class InputUtil {

    /**
     * 关闭软键盘
     *
     * @param context 上下文
     */
    @RequiresApi(api = Build.VERSION_CODES.CUPCAKE)
    public static void closeInput(Context context) {
        //得到InputMethodManager的实例
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        //如果开启
        if (imm.isActive()) {
            //关闭软键盘，开启方法相同，这个方法是切换开启与关闭状态的
            imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * 隐藏软键盘
     *
     * @param v View对象
     */
    @RequiresApi(api = Build.VERSION_CODES.CUPCAKE)
    public static void hideKeyboard(View v) {
        InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive()) {
            imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);

        }
    }

    /**
     * 永久关闭
     *
     * @param context 上下文
     */
    public static void closeInput2(Activity context) {
        //隐藏软键盘
        int flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
        context.getWindow().addFlags(flags);
    }

    /**
     * 显示软键盘
     *
     * @param context 上下文
     * @param view view对象
     */
    @RequiresApi(api = Build.VERSION_CODES.CUPCAKE)
    public static void showInput(Context context, View view) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
    }

    /**
     * 光标跳到文字的结尾
     *
     * @param et EditText控件对象
     */
    public static void inputLast(EditText et) {
        CharSequence text = et.getText();
        if (text instanceof Spannable) {
            Spannable spanText = (Spannable) text;
            Selection.setSelection(spanText, text.length());
        }
    }
    // =================新增方法===========================//
    /**
     * 打开软键盘
     *
     * @param mEditText EditText控件对象
     * @param mContext 上下文
     */
    @RequiresApi(api = Build.VERSION_CODES.CUPCAKE)
    public static void openKeybord(EditText mEditText, Context mContext) {
        mEditText.setFocusable(true);
        mEditText.setFocusableInTouchMode(true);
        mEditText.requestFocus();

        InputMethodManager imm = (InputMethodManager) mContext
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
                InputMethodManager.HIDE_IMPLICIT_ONLY);
    }

    /**
     * 关闭软键盘
     *
     * @param mEditText EditText控件对象
     * @param mContext 上下文
     */
    @RequiresApi(api = Build.VERSION_CODES.CUPCAKE)
    public static void closeKeybord(EditText mEditText, Context mContext) {
        InputMethodManager imm = (InputMethodManager) mContext
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
    }

    /**
     * 判断当前软键盘是否打开
     */
    @RequiresApi(api = Build.VERSION_CODES.CUPCAKE)
    public static boolean isSoftInputShow(Activity activity) {

        // 虚拟键盘隐藏 判断view是否为空
        View view = activity.getWindow().peekDecorView();
        if (view != null) {
            // 隐藏虚拟键盘
            InputMethodManager inputmanger = (InputMethodManager) activity
                    .getSystemService(Activity.INPUT_METHOD_SERVICE);

            return inputmanger.isActive() && activity.getWindow().getCurrentFocus() != null;
        }
        return false;
    }

    // =================新增方法===========================//
}
